﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using System.Reflection;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Setting_NewEditActionViewModel : CCommandViewModel
    {
        cWinKStartAction _Data = null;
        List<KeyValueAssignment> _ActionTypeItems = null;
        List<KeyValueAssignment> _EventSourceElement = null;
        List<KeyValueAssignment> _Functions = null;
        List<SourceElement> _SourceElements = null;
        List<KeyValueAssignment> _ContextMenuSourceElements = null;
        List<KeyValueAssignment> _GUIElements = null;
        List<Trigger> _TriggerItems = null;
        Trigger _SelectedTrigger = null;
        CContentConfig _Config = null;
        SourceElement _SelectedSourceElement = null;
        string _ActionName = "";
        string _SelectedActionType = "";
        string _SelectedEventSourceElement = "";
        string _SelectedFunction = "";
        bool _CanEditDeleteSourceElement = false;
        bool _CanEditDeleteTrigger = false;
        bool _AppliedChanges = false;
        bool _EnableNameChanging = true;

        public Setting_NewEditActionViewModel(CContentConfig Config)
        {
            //New Action
            base.ViewName = LanguageStrings.GetText("New Action");
            _Config = Config;
            _Data = new cWinKStartAction();

            InitActionTypeItems();
            InitEventSourceElements();
            InitFunctions();

            PropertyChanged += Setting_NewEditActionViewModel_PropertyChanged;

            _SourceElements = new List<SourceElement>();
            _TriggerItems = new List<Trigger>();



            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddSourceElementItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_EditSourceElementItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteSourceElementItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Apply"), new CDelegatingCommand(Delegate => this.Cmd_ApplyChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Ok"), new CDelegatingCommand(Delegate => this.CMd_OkDialog())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddTriggerItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_EditTriggerItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteTriggerItem())));

        }

        

        public Setting_NewEditActionViewModel(CContentConfig Config,cWinKStartAction Data)
        {
            base.ViewName = LanguageStrings.GetText("Edit Action");
            _Config = Config;
            _Data = Data;

            InitActionTypeItems();
            InitEventSourceElements();
            InitFunctions();

            PropertyChanged += Setting_NewEditActionViewModel_PropertyChanged;

            SelectedActionType = _Data.Type.ToString();
            _SourceElements = _Data.SourceElements;
            _ActionName = _Data.Name;


            _SelectedEventSourceElement = _Data.EventSourceElement;
            _SelectedFunction = _Data.Function;

            _TriggerItems = _Data.Triggers.Values.ToList();
            _EnableNameChanging = false;
            //_Data.Triggers


            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddSourceElementItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_EditSourceElementItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteSourceElementItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Apply"), new CDelegatingCommand(Delegate => this.Cmd_ApplyChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Ok"), new CDelegatingCommand(Delegate => this.CMd_OkDialog())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddTriggerItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Edit"), new CDelegatingCommand(Delegate => this.Cmd_EditTriggerItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteTriggerItem())));

        }


        #region Properties

        public cWinKStartAction Data
        {
            get { return _Data; }            
        }

        public CSingleCommandViewModel Cmd_Ok
        {
            get { return m_oCommands[5]; }
        }

        public CSingleCommandViewModel Cmd_AddSourceElement
        {
            get { return m_oCommands[0]; }
        }

        public CSingleCommandViewModel Cmd_EditSourceElement
        {
            get { return m_oCommands[1]; }
        }

        public CSingleCommandViewModel Cmd_DeleteSourceElement
        {
            get { return m_oCommands[2]; }
        }

        public CSingleCommandViewModel Cmd_AddTrigger
        {
            get { return m_oCommands[6]; }
        }

        public CSingleCommandViewModel Cmd_EditTrigger
        {
            get { return m_oCommands[7]; }
        }

        public CSingleCommandViewModel Cmd_DeleteTrigger
        {
            get { return m_oCommands[8]; }
        }

        public CSingleCommandViewModel Cmd_Apply
        {
            get { return m_oCommands[3]; }
        }

        public CSingleCommandViewModel Cmd_Discard
        {
            get { return m_oCommands[4]; }
        }

        public List<KeyValueAssignment> ActionTypeItems
        {
            get { return _ActionTypeItems; }            
        }

        public List<KeyValueAssignment> EventSourceItems
        {
            get { return _EventSourceElement; }
            set
            {
                _EventSourceElement = value;
                OnPropertyChanged("EventSourceItems");
            }
        }

        public List<KeyValueAssignment> FunctionItems
        {
            get { return _Functions; }
            set
            {
                _Functions = value;
                OnPropertyChanged("FunctionItems");
            }
        }

        public List<SourceElement> SourceElementItems
        {
            get { return _SourceElements; }
            set
            {
                _SourceElements = value;
                OnPropertyChanged("SourceElementItems");
            }
        }

        public List<Trigger> UsedTriggerItems
        {
            get { return _TriggerItems; }
            set
            {
                _TriggerItems = value;
                OnPropertyChanged("UsedTriggerItems");
            }
        }

        public string ActionName
        {
            get { return _ActionName; }
            set
            {
                _ActionName = value;
                OnPropertyChanged("ActionName");
            }
        }

        public string SelectedActionType
        {
            get { return _SelectedActionType; }
            set
            {
                _SelectedActionType = value;
                OnPropertyChanged("SelectedActionType");
            }
        }

        public string SelectedEventSource
        {
            get { return _SelectedEventSourceElement; }
            set
            {
                _SelectedEventSourceElement = value;
                OnPropertyChanged("SelectedEventSource");
            }
        }

        public string SelectedFunction
        {
            get { return _SelectedFunction; }
            set
            {
                _SelectedFunction = value;
                OnPropertyChanged("SelectedFunction");
            }
        }

        public SourceElement SelectedSourceElement
        {
            get { return _SelectedSourceElement; }
            set
            {
                _SelectedSourceElement = value;
                OnPropertyChanged("SelectedSourceElement");
            }
        }

        public Trigger SelectedTrigger
        {
            get { return _SelectedTrigger; }
            set
            {
                _SelectedTrigger = value;
                OnPropertyChanged("SelectedTrigger");
            }
        }

        public bool CanEditDeleteSourceElement
        {
            get { return _CanEditDeleteSourceElement; }
            set
            {
                _CanEditDeleteSourceElement = value;
                OnPropertyChanged("CanEditDeleteSourceElement");
            }
        }

        public bool CanEditDeleteTrigger
        {
            get { return _CanEditDeleteTrigger; }
            set {
                _CanEditDeleteTrigger = value;
                OnPropertyChanged("CanEditDeleteTrigger");
            }
        }

        public bool EnableNameChanging
        {
            get { return _EnableNameChanging; }
        }

        #endregion

        #region Command Procedures
        void Cmd_AddSourceElementItem() 
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddSourceElement.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddSourceElement.Command).CommandParameter;

                    Setting_AddActionSourceElementViewModel tmpSourceElement = null;

                    tmpSourceElement = new Setting_AddActionSourceElementViewModel();
                    Setting_AddActionSourceElement NewSourceElementDlg = new Setting_AddActionSourceElement();
                    NewSourceElementDlg.DataContext = tmpSourceElement;
                    if (NewSourceElementDlg.ShowDialog() == true)
                    {
                        SourceElement tmpDataItem = ((Setting_AddActionSourceElementViewModel)NewSourceElementDlg.DataContext).Data;
                        if (tmpDataItem != null)
                        {
                            if (FindSourceElement(tmpDataItem.Element) == false)
                            {
                                SourceElementItems.Add(tmpDataItem);
                                OnPropertyChanged("SourceElementItems");

                                tmpListView.Items.Refresh();
                            }
                        }
                    }

                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable add a new Source-Element!", Ex, EMessageTypes.Error);
            }
        }

        void Cmd_EditSourceElementItem() 
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditSourceElement.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditSourceElement.Command).CommandParameter;

                    Setting_AddActionSourceElementViewModel tmpSourceElement = null;
                    if (SelectedSourceElement != null)
                    {
                        tmpSourceElement = new Setting_AddActionSourceElementViewModel(SelectedSourceElement);
                        Setting_AddActionSourceElement NewSourceElementDlg = new Setting_AddActionSourceElement();
                        NewSourceElementDlg.DataContext = tmpSourceElement;
                        if (NewSourceElementDlg.ShowDialog() == true)
                        {
                            SelectedSourceElement = ((Setting_AddActionSourceElementViewModel)NewSourceElementDlg.DataContext).Data;
                            tmpListView.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to edit Source-Element!", Ex, EMessageTypes.Error);
            }
        }

        void Cmd_DeleteSourceElementItem() 
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteSourceElement.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteSourceElement.Command).CommandParameter;
                    
                    if (SelectedSourceElement != null)
                    {
                        if (SourceElementItems.Contains(SelectedSourceElement) == true)
                        {
                            if (System.Windows.MessageBox.Show(LanguageStrings.GetText("Delete selected Source-Element?", EStringTypes.stQuestion),
                                                               LanguageStrings.GetText("Question"), 
                                                               System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                            {
                                SourceElementItems.Remove(SelectedSourceElement);
                                tmpListView.Items.Refresh();
                            }
                        }

                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to edit Source-Element!", Ex, EMessageTypes.Error);
            }
        }

        void Cmd_AddTriggerItem()
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddTrigger.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_AddTrigger.Command).CommandParameter;

                    Setting_AddActionSourceElementViewModel tmpSourceElement = null;

                    Setting_AddActionTriggerViewModel tmpViewModel = new Setting_AddActionTriggerViewModel(_SelectedEventSourceElement, _SelectedActionType);
                    tmpSourceElement = new Setting_AddActionSourceElementViewModel();
                    Setting_AddActionTrigger NewSourceElementDlg = new Setting_AddActionTrigger();
                    NewSourceElementDlg.DataContext = tmpViewModel;
                    
                    if (NewSourceElementDlg.ShowDialog() == true)
                    {
                        Trigger tmpDataItem = ((Setting_AddActionTriggerViewModel)NewSourceElementDlg.DataContext).Data;
                        if (tmpDataItem != null)
                        {
                            if (FindTrigger(tmpDataItem.Event.ToString()) == false)
                            {
                                UsedTriggerItems.Add(tmpDataItem);
                                OnPropertyChanged("UsedTriggerItems");

                                tmpListView.Items.Refresh();
                            }
                        }
                    }

                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to add new Trigger!", Ex, EMessageTypes.Error);
            }
        }

        void Cmd_EditTriggerItem() 
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditTrigger.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_EditTrigger.Command).CommandParameter;

                    Setting_AddActionSourceElementViewModel tmpSourceElement = null;

                    Setting_AddActionTriggerViewModel tmpViewModel = new Setting_AddActionTriggerViewModel(_SelectedTrigger, _SelectedEventSourceElement, _SelectedActionType);
                    tmpSourceElement = new Setting_AddActionSourceElementViewModel();
                    Setting_AddActionTrigger NewSourceElementDlg = new Setting_AddActionTrigger();
                    NewSourceElementDlg.DataContext = tmpViewModel;

                    if (NewSourceElementDlg.ShowDialog() == true)
                    {
                        Trigger tmpDataItem = ((Setting_AddActionTriggerViewModel)NewSourceElementDlg.DataContext).Data;
                        if (tmpDataItem != null)
                        {
                            _SelectedTrigger = tmpDataItem;
                            tmpListView.Items.Refresh();
                            
                        }
                    }

                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to edit Trigger!", Ex, EMessageTypes.Error);
            }
        }

        void Cmd_DeleteTriggerItem()
        {
            try
            {
                System.Windows.Controls.ListView tmpListView = null;
                if ((System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteTrigger.Command).CommandParameter != null)
                {
                    tmpListView = (System.Windows.Controls.ListView)((CDelegatingCommand)Cmd_DeleteTrigger.Command).CommandParameter;

                    if (_SelectedTrigger != null)
                    {
                        if (System.Windows.MessageBox.Show(LanguageStrings.GetText("Delete selected Trigger?", EStringTypes.stQuestion),
                                                               LanguageStrings.GetText("Question"),
                                                               System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                        {
                            _TriggerItems.Remove(_SelectedTrigger);
                            OnPropertyChanged("TriggerItems");
                            tmpListView.Items.Refresh();
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to delete Trigger!", Ex, EMessageTypes.Error);
            }
        }

        void Cmd_ApplyChanges() 
        {
            try
            {
                ApplyChanges();
                _AppliedChanges = true;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to apply changes!", Ex, EMessageTypes.Error);
            }
        }

        void CMd_DiscardChanges() 
        {
            try
            {
                Settings_NewEditAction ParentWindow = null;
                if ((Settings_NewEditAction)((CDelegatingCommand)Cmd_Discard.Command).CommandParameter != null)
                {
                    ParentWindow = (Settings_NewEditAction)((CDelegatingCommand)Cmd_Discard.Command).CommandParameter;

                    if (ParentWindow != null)
                    {
                        if (_AppliedChanges == true)
                            ParentWindow.DialogResult = true;
                        ParentWindow.Close();
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on New/Edit Action!", Ex, EMessageTypes.Error);
            }
        }

        void CMd_OkDialog() 
        {
            try
            {
                Settings_NewEditAction ParentWindow = null;
                if ((Settings_NewEditAction)((CDelegatingCommand)Cmd_Ok.Command).CommandParameter != null)
                {
                    ParentWindow = (Settings_NewEditAction)((CDelegatingCommand)Cmd_Ok.Command).CommandParameter;

                    if (ParentWindow != null)
                    {
                        ApplyChanges();
                        ParentWindow.DialogResult = true;
                        ParentWindow.Close();
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Error on New/Edit Action!", Ex, EMessageTypes.Error);
            }
        }
        #endregion

        #region Helpers 

        void ApplyChanges()
        {
            try
            {
                if (_Data != null)
                {
                    _Data.Name = _ActionName;
                    _Data.Type = (eEventSourceType)Enum.Parse(typeof(eEventSourceType), _SelectedActionType);
                    _Data.EventSourceElement = _SelectedEventSourceElement;
                    _Data.Function = _SelectedFunction;
                    _Data.SourceElements = _SourceElements;
                    _Data.Triggers.Clear();
                    foreach (Trigger Item in _TriggerItems)
                    {
                        _Data.Triggers.Add(Item.Event.ToString(), Item);
                    }

                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to apply changes!", Ex, EMessageTypes.Error);
            }
        }

        void Setting_NewEditActionViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "SelectedActionType")
                {
                    if (_SelectedActionType.ToUpper() == "CONTEXTMENU")
                    {
                        //User will show Context-menu-items
                        EventSourceItems = _ContextMenuSourceElements;
                        SelectedEventSource = "";
                    }
                    else
                    {
                        //user will select gui elements
                        EventSourceItems = Meta_Elements.GetGUIElements("PROPERTIES");
                        SelectedEventSource = "";
                    }
                    //TODO: implements code to write available elements to combobox
                }
                else if (e.PropertyName == "SelectedSourceElement")
                {
                    if (_SelectedSourceElement != null)
                        CanEditDeleteSourceElement = true;
                    else
                        CanEditDeleteSourceElement = false;
                }
                else if (e.PropertyName == "SelectedTrigger")
                {
                    if (_SelectedTrigger != null)
                        CanEditDeleteTrigger = true;
                    else
                        CanEditDeleteTrigger = false;
                }

            }
            catch (Exception Ex) { }
        }
        
        void InitActionTypeItems()
        {
            try
            {
                _ActionTypeItems = new List<KeyValueAssignment>();
                _ActionTypeItems.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose a Type")));
                _ActionTypeItems.Add(new KeyValueAssignment(eEventSourceType.ContextMenu.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eEventSourceType.ContextMenu, "ContextMenu"))));
                _ActionTypeItems.Add(new KeyValueAssignment(eEventSourceType.Control.ToString(), LanguageStrings.GetText(EnumUtils.GetEnumValueDesc(eEventSourceType.Control, "Control"))));
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to Init Action Types!", Ex, EMessageTypes.Error);
            }
        }

        void InitEventSourceElements()
        {
            try
            {
                _ContextMenuSourceElements = new List<KeyValueAssignment>();
                _EventSourceElement = new List<KeyValueAssignment>();
                _GUIElements = new List<KeyValueAssignment>();
                _ContextMenuSourceElements.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose a Event source")));
                _EventSourceElement.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose a Event source")));
                _GUIElements.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose a Event source")));
                string sItemCaption = "";
                foreach (CContextMenuTargetItem Item in _Config.ContextMenuTargets)
                {
                    foreach (cWinKStartContextMenuItem MenuItem in Item.CustomMenuItems)
                    {
                        //cWinKStartContextMenuItem
                        sItemCaption = "(" + Item.Name + ") ";
                        sItemCaption += LanguageStrings.GetText(MenuItem.Caption);
                        _ContextMenuSourceElements.Add(new KeyValueAssignment(MenuItem.Name, sItemCaption));
                        _EventSourceElement.Add(new KeyValueAssignment(MenuItem.Name, "Context-Menu: " + sItemCaption));
                    }
                }

                Meta_Elements.LoadElements();
                //Meta_Elements.GetGUIElements("TEXTBOX");

            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to Init Event Source Elements!", Ex, EMessageTypes.Error);
            }
        }

        void InitFunctions()
        {
            try
            {
                _Functions = new List<KeyValueAssignment>();
                _Functions.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose a Function")));
                foreach (cWinKStartFunction Function in _Config.Functions)
                {
                    _Functions.Add(new KeyValueAssignment(Function.Name, Function.Name));
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage("Unable to Init Functions!", Ex, EMessageTypes.Error);
            }
        }

        bool FindSourceElement(string Name)
        {
            try
            {
                var SearchResult = from Item in _SourceElements where Item.Element.ToUpper() == Name.ToUpper() select Item;

                if (SearchResult != null && SearchResult.Count() > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception Ex)
            {
                throw new Exception("Error while Testing SourceElements exits!", Ex);
            }
        }
        bool FindTrigger(string Name)
        {
            try
            {
                var SearchResult = from Item in _TriggerItems where Item.Event.ToString().ToUpper() == Name.ToUpper() select Item;

                if (SearchResult != null && SearchResult.Count() > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception Ex)
            {
                throw new Exception("Error while Testing Trigger exits!", Ex);
            }
        }
 
        #endregion
    }
}
